/*
 * @Author: szx
 * @Date: 2021-12-27 14:18:11
 * @LastEditTime: 2021-12-27 15:12:16
 * @Description:
 * @FilePath: \leetcode\800-899\825\825.js
 */
/**
 * @param {number[]} ages
 * @return {number}
 */
var numFriendRequests = function (ages) {
    ages.sort((a, b) => b - a);
    const len = ages.length;
    let res = 0;
    for (let i = 0; i < len; i++) {
        const x = ages[i];
        for (let j = i + 1; j < len; j++) {
            const y = ages[j];
            if (y <= 0.5 * x + 7) {
                continue;
            }
            if (y <= 14) break;
            if (x == y) {
                res++;
            }
            res++;
        }
    }
    return res;
};

var numFriendRequests = function (ages) {
    const n = ages.length;
    ages.sort((a, b) => a - b);
    let left = 0,
        right = 0,
        ans = 0;
    for (const age of ages) {
        if (age < 15) {
            continue;
        }
        while (ages[left] <= 0.5 * age + 7) {
            ++left;
        }
        while (right + 1 < n && ages[right + 1] <= age) {
            ++right;
        }
        ans += right - left;
    }
    return ans;
};

console.log(numFriendRequests([16, 16]));
